package com.ny.demo524;

import com.ny.demo524.SqlConnection;

import java.sql.*;
import java.util.Scanner;

class LoginDemo {
    private static String username;
    private static String password;

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = SqlConnection.getCon();
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名称");
            username = scanner.next();
            System.out.println("请输入密码");
            password = scanner.next();
            String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String name = resultSet.getString("username");
                String pwd = resultSet.getString("password");
                System.out.println("用户名是：" + name);
                System.out.println("密码是：" + pwd);
            } else {
                System.out.println("登录失败");
            }

            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            System.err.println("数据库驱动未找到: " + e.getMessage());
        } catch (SQLException e) {
            System.err.println("数据库操作异常: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}